package cn.xiaopengstack.domain.trade.repository;

import cn.xiaopengstack.domain.trade.model.aggregate.GroupBuyOrderRefundAggregate;
import cn.xiaopengstack.domain.trade.model.aggregate.LockOrderAggregate;
import cn.xiaopengstack.domain.trade.model.aggregate.SettlementOrderAggregate;
import cn.xiaopengstack.domain.trade.model.entity.*;
import cn.xiaopengstack.domain.trade.model.valobj.GroupBuyOrderStatusEnum;

import java.util.List;
import java.util.Optional;

/**
 * @author jiangyangang
 */
public interface ITradeRepository {

    /**
     * 查询未支付的拼团订单
     * @param userId
     * @param outOrderId
     * @return
     */
    Optional<MarketOrderEntity> queryUnPayMarketPayOrder(String userId, String outOrderId);

    /**
     * 查询
     * @param teamId
     * @return
     */
    Optional<ProgressEntity> queryGoodsGroupByProgress(String teamId);

    /**
     * 锁定拼团营销订单
     * @param aggregate
     * @return
     */
    MarketOrderEntity lockGroupBuyMarketOrder(LockOrderAggregate aggregate) throws Exception;

    /**
     * 查询拼团订单
     * @param teamId
     * @return
     */
    Optional<GroupBuyOrderEntity> queryOrderByTeamId(String teamId);

    /**
     * 更新拼团订单表状态
     * @param teamId
     * @param statusEnum
     * @return
     */
    boolean updateStatusOrderByTeamId(String teamId, GroupBuyOrderStatusEnum statusEnum);

    /**
     * 结算拼团营销订单
     * @param aggregate
     * @return
     */
    void settlementOrder(SettlementOrderAggregate aggregate);

    /**
     * 判断是否是黑名单渠道
     * @param source
     * @param channel
     * @return
     */
    boolean isSCBlack(String source, String channel);

    /**
     * 查找还没有执行的通知
     * @return
     */
    List<NotifyTaskEntity> queryUnExecutedNotifyTaskList();

    /**
     * 查找还没有执行的通知
     * @param teamId
     * @return
     */
    List<NotifyTaskEntity> queryUnExecutedNotifyTaskList(String teamId);

    /**
     * 更新通知状态为成功
     * @param notifyTaskEntity
     * @return
     */
    int updateNotifySuccess(NotifyTaskEntity notifyTaskEntity);

    /**
     * 更新通知状态为失败
     * @param notifyTaskEntity
     * @return
     */
    int updateNotifyError(NotifyTaskEntity notifyTaskEntity);

    /**
     * 更新通知状态为重试
     * @param notifyTaskEntity
     * @return
     */
    int updateNotifyRetry(NotifyTaskEntity notifyTaskEntity);

    /**
     * 抢占库存
     * @param teamId
     * @param activityId
     * @param targetCount
     * @param validTime
     * @return
     */
    boolean occupyTeamStock(String teamId, Long activityId, Long targetCount, Long validTime);

    /**
     * 结算成功通知
     * @param notifyTaskEntity
     * @return
     */
    int tradeSettlementNotify(NotifyTaskEntity notifyTaskEntity);

    /**
     * 获取拼团明细
     * @param orderId
     * @return
     */
    Optional<GroupBuyOrderListEntity> queryOrderDetailByOrderId(String orderId);

    /**
     * 订单退单
     * @param groupBuyOrderRefundAggregate
     */
    void unlockOrder(GroupBuyOrderRefundAggregate groupBuyOrderRefundAggregate);

    /**
     * 订单退单，订单已经支付，但是拼团还在进行
     * @param groupBuyOrderRefundAggregate
     */
    void refundOrder(GroupBuyOrderRefundAggregate groupBuyOrderRefundAggregate);


    /**
     * 订单退款，订单已经支付，整单全部退款
     * @param groupBuyOrderRefundAggregate
     */
    void refundTeam(GroupBuyOrderRefundAggregate groupBuyOrderRefundAggregate);


    /**
     * 查询退款订单
     * @param orderId
     * @param userId
     * @return
     */
    Optional<GroupBuyOrderRefundEntity> queryRefundByOrderId(String orderId, String userId);

}
